require('./index.css');
require('page/common/top/index.js');
var _util = require('util/util.js');
var listTemplate = require('./index.string');
var _catalog_service = require('service/catalog-service.js');
var _account_service = require('service/account-service.js');


var catalogDetail = {
    listData: {
        description: '',
        itemId : '',
        attribute1 : '',
        name : '',
        listPrice : '',
    },
    init: function () {
        this.loadAccountInfo();
        // this.bindEvents();
        return this;
    },
    bindEvents: function () {
        $('.add_Btn').on("click", function () {
            var _this = this
            _account_service.checkLogin(
                function (res) {
                    var itemId = $(_this).parent().parent().parent().children('tr').first().next().children().text();
                    _catalog_service.addToCart(
                        itemId,
                        function (res) {
                            window.location.href = './cart.html';
                        },
                        function (err) {
                            console.log("ERROR...")
                        }
                    )
                },
                function (err) {
                    alert("请先登录")
                    window.location.href = './user-login.html';
                }
            )
        }),
        $('#return').on("click",function(){
            window.location.href = './catalog-main.html';
        })

    },
    loadAccountInfo: function () {
        var id = _util.getURLParam('id');
        console.log('id');
        var _this = this;

        _catalog_service.getItemVOById(id,
            function (res) {
                console.log(res)
                _this.listData.description = res.productDescription;
                _this.listData.itemId = res.itemId;
                _this.listData.attribute1 = res.attribute1;
                _this.listData.name = res.productName;
                _this.listData.listPrice = res.listPrice;
                _this.listData.quantiy = res.quantiy;

                var result = _util.renderHtml(listTemplate, { ListData: _this.listData })
                console.log(result);
                $('#Catalog').html(result);

                $('#imgDes').html(res.productDescription)
                _this.bindEvents();
            },
            function () {

            }
        )
    }
};

module.exports = catalogDetail.init()